package com.thitech.sif.interceptor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.thitech.sif.Constant;
import com.thitech.sif.exception.SifException;

/**
 * 拦截器工厂
 * 
 * @author shisheng.lian
 * @date 2014-08-06
 * 
 */
public class InterceptorFactory {
	// 全局默认异常处理拦截器
	public static final String GLOBAL_DEFAULT_EXCEPTION = "GLOBAL.DEFAULT.EXCEPTION";
	public static final InterceptorHandler GLOBAL_DEFAULT_EXCEPTION_INTERCEPTOR = new ExceptionInterceptor();

	// 日志记录拦截器
	public static final String GLOBAL_LOG = "GLOBAL.LOG";
	public static final InterceptorHandler GLOBAL_LOG_INTERCEPTOR = new LogInterceptor();

	// 返回值判断拦截器
	public static final String GLOBAL_RETURN_ASSERT = "GLOBAL.RETURN.ASSERT";
	public static final InterceptorHandler GLOBAL_RETURN_ASSERT_INTERCEPTOR = new ReturnAssertInterceptor();

	// 服务执行时间拦截器
	public static final String GLOBAL_INVOKE_TIME = "GLOBAL.INVOKE.TIME";
	public static final InterceptorHandler GLOBAL_INVOKE_TIME_INTERCEPTOR = new InvokeTimeInterceptor();

	private static Map<String, InterceptorHandler> interceptorMap = new HashMap<String, InterceptorHandler>();
	static {
		interceptorMap.put(GLOBAL_DEFAULT_EXCEPTION, GLOBAL_DEFAULT_EXCEPTION_INTERCEPTOR);
		interceptorMap.put(GLOBAL_LOG, GLOBAL_LOG_INTERCEPTOR);
		interceptorMap.put(GLOBAL_RETURN_ASSERT, GLOBAL_RETURN_ASSERT_INTERCEPTOR);
		interceptorMap.put(GLOBAL_INVOKE_TIME, GLOBAL_INVOKE_TIME_INTERCEPTOR);
	}
	
	public static InterceptorHandler getInterceptor(String interceptorName) {
		return interceptorMap.get(interceptorName);
	}

	/**
	 * 根据服务类获取用自定义拦截器之前的默认拦截器链
	 * 
	 * @param serviceType
	 * @return 返回默认拦截器链
	 */
	public static List<InterceptorHandler> getBeforeDefaultInterceptors(
			String serviceType) {
		List<InterceptorHandler> interceptors = new ArrayList<InterceptorHandler>();
		if (Constant.SERVICE_TYPE_SOCKET.equalsIgnoreCase(serviceType)) {
			interceptors.add(GLOBAL_DEFAULT_EXCEPTION_INTERCEPTOR);
		} else if (Constant.SERVICE_TYPE_WS.equalsIgnoreCase(serviceType)) {
			interceptors.add(GLOBAL_DEFAULT_EXCEPTION_INTERCEPTOR);
		} else {
			throw new SifException("根据服务类型获取默认拦截器时服务类型不正确，错误服务类型为："
					+ serviceType);
		}
		return interceptors;
	}

	/**
	 * 根据服务类获取用自定义拦截器之后的默认拦截器链
	 * 
	 * @param serviceType
	 * @return 返回默认拦截器链
	 */
	public static List<InterceptorHandler> getAfterDefaultInterceptors(
			String serviceType) {
		List<InterceptorHandler> interceptors = new ArrayList<InterceptorHandler>();
		if (Constant.SERVICE_TYPE_SOCKET.equalsIgnoreCase(serviceType)) {
			 interceptors.add(GLOBAL_INVOKE_TIME_INTERCEPTOR);
		} else if (Constant.SERVICE_TYPE_WS.equalsIgnoreCase(serviceType)) {
			 interceptors.add(GLOBAL_INVOKE_TIME_INTERCEPTOR);
		} else {
			throw new SifException("根据服务类型获取默认拦截器时服务类型不正确，错误服务类型为："
					+ serviceType);
		}
		return interceptors;
	}

	/**
	 * Socket拦截器类列表
	 * 
	 * @return
	 */
	public static List<InterceptorHandler> getSocketDefaultInterceptors() {
		List<InterceptorHandler> interceptors = new ArrayList<InterceptorHandler>();
		return interceptors;
	}

	/**
	 * web service拦截器类列表
	 * 
	 * @return
	 */
	public static List<InterceptorHandler> getWsDefaultInterceptors() {
		List<InterceptorHandler> interceptors = new ArrayList<InterceptorHandler>();
		return interceptors;
	}
}
